﻿@using LeadMaster.Helpers
@using LeadMaster.Properties
@model LeadMaster.ViewModels.ViewModelOutsourcer
@using LeadMaster.Controllers;

<style type="text/css">
.InfoRowHeight
{
    height: 20px;   
}
.sectionheader
{
    font-size: 9px;
    text-align: left;
 }
 .topsectiontitles
 {
    text-align: left;
    background-color: #BFFFFF;
 }
</style>

<div class="spacer"></div>

<div id="totalgrid">
@if (@Model.FullListLeadResults != null && @Model.FullListLeadResults.Count() > 0)
{
<div class="spacer"></div>
<table>
<tr>
<th>&nbsp;</th>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th>
    <label style="color: #FF0000; font-size: 9px; font-family: Arial, Helvetica, sans-serif; font-weight: bold;">
    @if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.ClientNote != null)
        {
            @Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.ClientNote
        }
    }
    </label>
    </th>    
}
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>

<tr>
<th class="topsectiontitles">&nbsp;</th>
@{
int colSpan = 1;
string blockTitle = "";
for (int i = 0; i < @Model.Titles.Count(); i++)
{
    if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID > 0)
    {
        if (blockTitle == Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.LeadStat.Title)
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>                    
            }
            colSpan = 1;
            blockTitle = Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.LeadStat.Title;
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (blockTitle == Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.LeadGroups.Title)
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>                
            }
            colSpan = 1;
            blockTitle = Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.LeadGroups.Title;
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID != null && Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID > 0)
    {
        if (blockTitle == "Registration")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Registration";
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID != null && Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID > 0)
    {
        if (blockTitle == "Restriction")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
            <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Restriction";
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanySundriesID != null && Model.FullListLeadResults[i].LeadResult.CompanySundriesID > 0)
    {
        if (blockTitle == "Sundries")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
            <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Sundries";
        }
    }
} 
<th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
}
<th class="topsectiontitles">&nbsp;</th>
<th class="topsectiontitles">&nbsp;</th>
<th class="topsectiontitles">&nbsp;</th>
<th class="topsectiontitles">&nbsp;</th>
</tr>
<tr>
<th style="background-color: #BFFFFF;">&nbsp;</th>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th style="background-color: #BFFFFF;">
    <label style="color: #FF0000;">
    @if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.Mandatory == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.Mandatory == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID != null && Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanyRegistered.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID != null && Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanyRestriction.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }    
    @if (Model.FullListLeadResults[i].LeadResult.CompanySundriesID != null && Model.FullListLeadResults[i].LeadResult.CompanySundriesID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanySundry.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }    
    </label>
    </th>    
}
<th style="background-color: #BFFFFF;">&nbsp;</th>
<th style="background-color: #BFFFFF;">&nbsp;</th>
<th style="background-color: #BFFFFF;">&nbsp;</th>
<th style="background-color: #BFFFFF;">&nbsp;</th>
</tr>
<tr>
<th style="border: 1px solid #000000; background-color: #999999;"><label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">Ref</label></th>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th style="border: 1px solid #000000; background-color: #999999;">
    <label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
    @Model.Titles[i]
    </label>
    </th>    
}

<th style="border: 1px solid #000000; background-color: #999999;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Current Status
</label>
</th>
@if (ViewBag.ImmmediateReview == false && ViewBag.ReleaseOnly == false)
{
<th style="border: 1px solid #000000; background-color: #999999;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Agents Notes On Submission
</label>
</th>
}
</tr>
<tr id="row@(Model.FullListLeadResults.FirstOrDefault().LeadStatus.BlockID)"  style="@(OutsourcerController.GetColourCode(Model.FullListLeadResults.FirstOrDefault().LeadFlag))">
@{
int iCounter = 0;
string poID = string.Empty;
bool startOfSequence = true;
bool finishSequence = false;
string colourCode = OutsourcerController.GetColourCode(Model.FullListLeadResults.FirstOrDefault().LeadFlag);
foreach (var item in Model.FullListLeadResults)
{
    if (startOfSequence)
    {
        @:<td style="border: 1px solid #000000;"><label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold;">@item.LeadStatus.BlockID</label></td>
        startOfSequence = false;
    }
    if (iCounter == item.LeadResult.QuestionBlock || finishSequence)
    {
        @:</tr><tr style="width: 25px; height: 25px"  id="row@(item.LeadStatus.BlockID)">
        @:<td style="border: 1px solid #000000;"><label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold;">@item.LeadStatus.BlockID</label></td>
        iCounter = 0;
        finishSequence = false;
    }
    <td style="border: 1px solid #000000;">
    <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold;">
    @item.LeadResult.Answer
    </label>
    </td>
    iCounter++;
    if (iCounter == item.LeadResult.QuestionBlock)
    {
        finishSequence = true;
        poID = item.LeadStatus.PurchaseOrderID.ToString();
        <text>
            <td style="border: 1px solid #000000;">
            @if (item.LeadFlag.LeadFlagsID == 1)
            {
                <input type="button" value="Release To Client" class="btnReleaseToClient" data-id="@(item.LeadStatus.LeadStatusID)"/>
            }
            else
            {
                <label style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; " data-id="@(item.LeadResult.LeadResultsID)">
                    @item.LeadFlag.Description
                </label>
            }
            </td>
            @if (ViewBag.ImmmediateReview == false && ViewBag.ReleaseOnly == false)
            {
            <td style="border: 1px solid #000000;">
            <textarea style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; width:250px;height: 80px;" readonly>@item.LeadStatus.Notes</textarea>
            </td>
            }
            @if (item.LeadStatus.LeadFlagsID == 1)
            {
            <td style="border: 1px solid #000000;">
            <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; cursor: pointer;" class="changeosnotes" data-id="@(item.LeadStatus.LeadStatusID)">
            @if (string.IsNullOrEmpty(item.LeadStatus.OutsourcerNotes))
            {
		    @:Enter a note
	        }
            else
            {
                @(item.LeadStatus.OutsourcerNotes)
            }
            </label>
            </td>
            }
            <td>
            @if (item.LeadFlag.LeadFlagsID == 1)
            {
                @:<input type="button" value="Edit" class="EditLead" data-leadid="@(item.LeadResult.LeadID)" data-blockid="@(item.LeadResult.BlockId)" />
            }
            else
            {
                @:&nbsp;
            }
            </td>
            <td>
            <a style='font-family: Arial, Helvetica, sans-serif; font-size: 9px; font-style: italic; color: #800080' href='https://www.google.co.uk/maps/place/@(QCController.StreetviewPostcode(item.LeadStatus.LeadStatusID))' target='_blank'>StreetView</a>
            <!--<a style='font-family: Arial, Helvetica, sans-serif; font-size: 9px; font-style: italic; color: #800080' href='https://www.epcregister.com/reportSearchAddressByPostcode.html' target='_blank'>EPC</a>-->
            </td>
            @if (ViewBag.ReleaseOnly == true || ViewBag.ImmmediateReview == true)
            {
		    <td><img src='../../Images/delete.ico' data-ref="@(item.LeadStatus.BlockID)" class="deletelead" style="height: 25px; width: 25px;"/></td>
            }
            </text>                

    }
}
}
</tr>
</table>
}
else if (ViewBag.FiltersSelected == true && ViewBag.ImmmediateReview == false && ViewBag.ReleaseOnly == false)
{
<h3>No Leads found.</h3>
}
</div>
<input type="hidden" id="refreshmessage" value="false" />
<div id="changestatus"></div>
<div id="changereason"></div>
<div id="osnote"></div>

<script type="text/javascript">
    $(document).ready(function () {

        $("#changestatus").dialog(
        {
            autoOpen: false,
            width: 600,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                save: function () {
                    $(this).dialog("close");
                    $("#formChangeLeadStatus").submit();
                },
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#changereason").dialog(
        {
            autoOpen: false,
            width: 600,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                save: function () {
                    $(this).dialog("close");
                    $("#formChangeReason").submit();
                },
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#osnote").dialog(
        {
            autoOpen: false,
            width: 600,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                save: function () {
                    $(this).dialog("close");
                    $("#formOsNote").submit();
                },
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $(".changeleadstatus").click(function () {
            $("#changestatus").dialog("open");
            $('#changestatus').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL = '@(Url.Action("ChangeLeadStatus", "Outsourcer"))/?id=' + $(this).data("id");
            $('#changestatus').load(contactURL);
        });

        $(".changeleadreason").click(function () {
            $("#changereason").dialog("open");
            $('#changereason').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL = '@(Url.Action("ChangeLeadReason", "Outsourcer"))/?id=' + $(this).data("id");
            $('#changereason').load(contactURL);
        });

        $(".changeosnotes").click(function () {
            $("#osnote").dialog("open");
            $('#osnote').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            var contactURL = '@(Url.Action("ChangeOsNote", "Outsourcer"))/?id=' + $(this).data("id") + '&immediateReview=@(ViewBag.ImmmediateReview)';
            $('#osnote').load(contactURL);
        });

        $(".btnReleaseToClient").click(function () {
            var contactURL = '@(Url.Action("ReleaseToClient", "Outsourcer"))/?id=' + $(this).data("id") + '&immediateReview=@(ViewBag.ImmmediateReview)';
            window.location.href = contactURL;
        });

        $(".EditLead").click(function () {
            var contactURL = '@(Url.Action("EditLeads", "Outsourcer"))?leadID=' + $(this).data("leadid") + '&blockID=' + $(this).data("blockid") + '&caption=' + $(this).val();
            window.location.href = contactURL;
        });

        $(".deletelead").click(function () {
            var ans = confirm("Are you sure you wish to delete this lead ?")
            if (ans == true) {
                $.ajax({
                    type: "GET",
                    url: '@(Url.Action("DeleteLead", "Outsourcer"))/?blockID=' + $(this).data("ref")
                });
                if ($("table tr").length == 5) {
                    $("#totalgrid").hide();
                }
            }
            $("#row" + $(this).data("ref")).remove();
        });
    });
</script>

